<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList">
        查询
      </el-button>
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleAdd">
        添加
      </el-button>
    </div>
      <el-table v-loading="listLoading" :data="list" border fit highlight-current-row row-key="id" style="width: 100%;">
        <el-table-column label="区域" prop="typeName" align="center" />
        <el-table-column label="广告图片" align="center">
          <template slot-scope="{row}">
            <img @click="handleOpenImage(row)" :src="row.image" min-width="70" height="70">
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    
    <el-dialog :title="title" v-if="dialogFormVisible" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="120px"
        style="margin: 0 50px;">
        <el-form-item label="区域" prop="type">
          <el-select style="width: 200px" v-model="temp.type" placeholder="区域" clearable>
            <el-option
              v-for="children in typeOption" :key="children.value" :label="children.label" :value="children.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图片(702*270)" prop="image">
          <UploadNoEditImage :image.sync="temp.image" :limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveData()">
          确定
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="品牌图片" width="550px" v-if="imageVisible" :visible.sync="imageVisible">
      <img style="width: 500px;" :src="goodImage">
    </el-dialog>
  </div>
</template>

<script>
import { getAdList, updateAd, removeAd } from '@/api/product'
import UploadNoEditImage from '@/components/UploadImage/noEdit.vue'
export default {
  name: 'ZcyxAdvertisement',
  components: { UploadNoEditImage },
  data() {
    return {
      list: [],
      listLoading: true,
      dialogFormVisible: false,
      listQuery: {
        pageNo: 1,
        pageSize: 20,
      },
      temp: {
        id: '',
        name: '',
        image: ''
      },
      rules: {
        type: [{ required: true, message: '请输入', trigger: 'change' }],
        image: [{ required: true, message: '请输入', trigger: 'change' }],
      },
      title: '',
      imageVisible: false,
      goodImage: '',
      typeOption: [
        {value: 1, label: '优选云仓'},
        {value: 2, label: '普洱名品'},
        {value: 3, label: '新品上架'},
        {value: 4, label: '茶礼盒'},
        {value: 5, label: '尾货专区'},
        {value: 6, label: '经销商专区'},
      ]
    }
  },
  computed: {
    
  },
  watch: {
    
  },
  created() {
    this.getList()
  },
  methods: {
    handleOpenImage(row) {
      this.goodImage = row.image
      this.imageVisible = true
    },
    closeViewer() {
      this.imageVisible = false
    },
    handleData() {
      for (let index = 0; index < this.list.length; index++) {
        var element = this.list[index];
        for (let j = 0; j < this.typeOption.length; j++) {
          var item = this.typeOption[j];
          if (element.type == item.value) {
            element.typeName = item.label
          }
        }
      }
    },
    getList() {
      this.listLoading = true;
      getAdList(this.listQuery).then((response) => {
        this.list = response || [];
        this.listLoading = false;

        this.handleData()
      });
    },
    handleFilter() {
      this.listQuery.pageNo = 1;
      this.getList();
    },
    handleAdd() {
      delete this.temp.createTime
      this.temp.id = ''
      this.temp.name = ''
      this.temp.image = ''
      this.dialogFormVisible = true
      this.title = "添加"
    },
    saveData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateAd(this.temp).then(() => {
            this.getList()
            this.dialogFormVisible = false

            this.$message({
              message: '保存成功',
              type: 'success'
            });
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否确定删除？').then(() => {
        removeAd({ids: row.id}).then(response => { 
          this.getList()
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        }).catch(error => {
          console.log(error);
        })
      })
    },
  }
}
</script>
<style lang="scss" scoped>


</style>